package fms.thread;

import fms.util.FtpUpDownLoadParamVo;
import fms.util.FtpUtils;
import fms.util.ObjectUtil;
import fms.util.StringUtil;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Desc: 本服务用于本地服务器与其他FTP服务器之间进行文件上传/下载功能
 */
public class FtpUpDownLoadThread implements Runnable {
    private static Logger ftpUpDownLoadLogger = Logger.getLogger("ftpUpDownLoad");

    // 用于标识当前线程是上传线程(up)还是下载线程(down)
    private String flagUpDown = null;
    private List updownLoadTaskList = null;

    public FtpUpDownLoadThread(List updownLoadTaskList, String flagUpDown) {
        this.updownLoadTaskList = updownLoadTaskList;
        this.flagUpDown = flagUpDown;
    }

    @Override
    public void run() {
        dealFtpUpDownLoad(updownLoadTaskList, flagUpDown);
    }

    /**
     *
     */
    private static void dealFtpUpDownLoad(List updownLoadTaskList, String flagUpDown) {
        try {
            if (!ObjectUtil.isBlank(flagUpDown)
                    && flagUpDown.equals("up")) {
                for (int i = 0; i < updownLoadTaskList.size(); i++) {
                    FtpUpDownLoadParamVo upLoadParamVo = (FtpUpDownLoadParamVo)updownLoadTaskList.get(i);

                    File fSacnDir = new File(upLoadParamVo.getLocaldir());
                    if ((null == fSacnDir) || !fSacnDir.isDirectory()) {
                        ftpUpDownLoadLogger.error("无法打开FTP上传文件所在的数据源目录：" + upLoadParamVo.getLocaldir());
                        return;
                    }
                    String[] fileNames = fSacnDir.list();
                    List fileNameList = StringUtil.stringArray2List(fileNames);
                    if (null != fileNameList && fileNameList.size() > 0) {
                        FtpUtils upFtpUtil = new FtpUtils(upLoadParamVo.getIp(), upLoadParamVo.getUserName(),
                                upLoadParamVo.getPassWord(), upLoadParamVo.getPort(), upLoadParamVo.getFtpdir());
                        upFtpUtil.connectServer();
                        upFtpUtil.uploadFile(upLoadParamVo.getLocaldir(), fileNameList, upLoadParamVo.getBackupdir());
                        upFtpUtil.closeServer();
                    }
                }
            } else if (!ObjectUtil.isBlank(flagUpDown)
                    && flagUpDown.equals("down")) {
                for (int i = 0; i < updownLoadTaskList.size(); i++) {
                    FtpUpDownLoadParamVo downLoadParamVo = (FtpUpDownLoadParamVo)updownLoadTaskList.get(i);

                    FtpUtils downFtpUtil = new FtpUtils(downLoadParamVo.getIp(), downLoadParamVo.getUserName(),
                            downLoadParamVo.getPassWord(), downLoadParamVo.getPort(), downLoadParamVo.getFtpdir());
                    downFtpUtil.connectServer();
                    List downFileNameList = downFtpUtil.getFileList(downLoadParamVo.getFtpdir());
                    downFtpUtil.downloadFile(downLoadParamVo.getLocaldir(),downFileNameList, downLoadParamVo.getBackupdir());
                    downFtpUtil.closeServer();
                }
            }
        } catch (Exception e) {
            ftpUpDownLoadLogger.error("FTP上传下载线程异常终止：" + e.toString());
        }
    }


}
